Code hoofdstuk 3

terug

8.2

/*from H.A. Lauwerier Graphics&Fractals, 1994 program TURTLEK translated into Processing by J.G.van Unnik, 2010*/ int NMAX=1; String AXIOM="F",PROD="F+F--F+F"; void setup(){   size(300,150);   background(255);   noLoop(); } void draw(){   float x=-3,y=-.5,h=6/pow(3,NMAX);   String WEG=AXIOM,W$,Q$,S$;   for(int n=1;n<=NMAX;n++){     W$="";     for(int i=0;i<WEG.length();i++){       S$=str(WEG.charAt(i));       if(S$.equals("F")){         Q$=PROD;}       else{         Q$=S$;}         W$=W$+Q$;     }     WEG=W$;   }      // graphics   translate(width/2,height/2);   float a=cos(PI/3),b=sin(PI/3);   float p=1,q=0,p1,q1,x1,y1;   for (int j=0;j<WEG.length();j++){     S$=str(WEG.charAt(j));     if(S$.equals("+")){       p1=a*p-b*q;       q1=b*p+a*q;       p=p1;q=q1;     }     else if(S$.equals("-")){       p1=a*p+b*q;       q1=-b*p+a*q;       p=p1;q=q1;     }     else if(S$.equals("F")){      x1=x+h*p;      y1=y+h*q;      line(50*x,-50*y,50*x1,-50*y1);      x=x1;y=y1;     }   } } void mousePressed(){   NMAX=NMAX+1;   if(NMAX>7){NMAX=1;}   background(255);   redraw(); }                  :

terug

8.4

/*from H.A. Lauwerier Graphics&Fractals, 1994 program TURTLEK1 translated into Processing by J.G.van Unnik, 2010*/ int NMAX=4; String AXIOM$="F",PROD$="F+F-F-F+F"; void setup(){   size(350,200);   background(255);   //strokeWeight(0);   noLoop(); } void draw(){   float x=-2,y=-1,h=4,phi=0;   String WEG=AXIOM$,W$,Q$,S$;   for(int n=1;n<=NMAX;n++){     W$="";     for(int i=0;i<WEG.length();i++){       S$=str(WEG.charAt(i));       if(S$.equals("F")){         Q$=PROD$;}       else{         Q$=S$;}         W$=W$+Q$;     }     WEG=W$;   }      // graphics   translate(10,10); //corresponds to QBASIC window(-4,-3)-(4,3)   //scale(1,-1); //   float a=cos(PI/3),b=sin(PI/3);   float p=1,q=0,p1,q1,x1,y1;   for (int j=0;j<WEG.length();j++){     S$=str(WEG.charAt(j));     if(S$.equals("+")){      phi=phi+HALF_PI;     }     else if(S$.equals("-")){      phi=phi-HALF_PI;     }     else if(S$.equals("F")){      x1=x+h*cos(phi);      y1=y+h*sin(phi);      line(x,y,x1,y1);      x=x1;y=y1;     }   } } void mousePressed(){   NMAX=NMAX+1;   if(NMAX>4){NMAX=1;}   background(255);   redraw(); }                  

terug

8.5
/*from H.A. Lauwerier Graphics&Fractals, 1994 program TURTLEK2 translated into Processing by J.G.van Unnik, 2010Von Koch Snowflake*/ int NMAX=4; String AXIOM$="F+F+F+F+F+F",PROD$="F+F--F+F"; void setup(){   size(300,300);   background(255);   strokeWeight(0.04);   noLoop(); } void draw(){   float x=-100,y=-150,h=100/pow(3,NMAX);   String WEG=AXIOM$,W$,Q$,S$;   for(int n=1;n<=NMAX;n++){     W$="";     for(int i=0;i<WEG.length();i++){       S$=str(WEG.charAt(i));       if(S$.equals("F")){         Q$=PROD$;}       else{         Q$=S$;}         W$=W$+Q$;     }     WEG=W$;   }      translate(width/2,height/2); //corresponds to QBASIC window(-4,-3)-(4,3)   //scale(35,-35);   for(int n=0;n<4;n++){   h=.7*h;x=.7*x;y=.7*y;   float a=cos(PI/3),b=sin(PI/3);   float p=1,q=0,p1,q1,x1,y1;   for (int j=0;j<WEG.length();j++){     S$=str(WEG.charAt(j));     if(S$.equals("+")){       p1=a*p-b*q;       q1=b*p+a*q;       p=p1;q=q1;     }     else if(S$.equals("-")){       p1=a*p+b*q;       q1=-b*p+a*q;       p=p1;q=q1;     }     else if(S$.equals("F")){      x1=x+h*p;      y1=y+h*q;      line(x,y,x1,y1);      x=x1;y=y1;     }     }   }    } void mousePressed(){   NMAX=NMAX+1;   if(NMAX>5){NMAX=1;}   background(255);   redraw(); }

terug

8.9

/*from H.A. Lauwerier Graphics&Fractals, 1994 program TURTLEK3 translated into Processing by J.G.van Unnik, 2010*/ //Kochs' island int NMAX=2,zoom=90; float x=-1.5,y=0.5; int[]X=new int[50]; int[]Y=new int[972]; String AXIOM$="FRFRFRFR",PROD$="-FLFRF+"; void setup(){   size(300,300);   background(255);   fill(0,0,255);   strokeWeight(0);   noLoop();   smooth();   } void draw(){   float a=2/sqrt(5),b=1/sqrt(5);   float h=2*pow(b,NMAX);   String WEG=AXIOM$,W$,Q$,S$;   for(int n=1;n<=NMAX;n++){     W$="";     for(int i=0;i<WEG.length();i++){       S$=str(WEG.charAt(i));       if(S$.equals("F")){         Q$=PROD$;}       else{         Q$=S$;}         W$=W$+Q$;     }     WEG=W$;   }      // graphics   translate(width/2,height/2); //corresponds to QBASIC window(-4,-3)-(4,3) //   int m=0; //counter for number of sides   float p=1,q=0,p1,q1;   for (int j=1;j<WEG.length();j++){     S$=str(WEG.charAt(j));     if(S$.equals("+")){       p1=a*p-b*q;       q1=b*p+a*q;       p=p1;q=q1;     }     else if(S$.equals("L")){       p1=-q;q1=p;       p=p1;q=q1;     }     else if(S$.equals("R")){       p1=q;q1=-p;       p=p1;q=q1;     }     else if(S$.equals("-")){       p1=a*p+b*q;       q1=-b*p+a*q;       p=p1;q=q1;     }     else if(S$.equals("F")){      x=x+h*p;      y=y+h*q;      X[m]=int(zoom*x);Y[m]=int(zoom*y);      m=m+1;     }   } beginShape();   for(int n=0;n<m;n++){     vertex(X[n],Y[n]);   }  endShape(CLOSE); }  void mousePressed(){   NMAX=NMAX+1;   if(NMAX>5){NMAX=1;}   background(255);   redraw(); }